public class ThreadPrint {
    static volatile int  count = 0;
    static Object locker = new Object();

    public static void main(String[] args) {
        Thread t3 = new Thread(()->{
            for (int i = 0; i < 10; i++) {
                synchronized (locker){
                    while(count%3!=2){
                        try {
                            locker.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println(Thread.currentThread().getName());
                    count++;
                    locker.notifyAll();
                }
            }
        },"C");
        Thread t1 = new Thread(()->{
            for (int i = 0; i < 10; i++) {
                synchronized (locker){
                    while(count%3!=0){
                        try {
                            locker.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.print(Thread.currentThread().getName());
                    count++;
                    locker.notifyAll();
                }
            }
        },"A");
        Thread t2 = new Thread(()->{
            for (int i = 0; i < 10; i++) {
                synchronized (locker){
                    while(count%3!=1){
                        try {
                            locker.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.print(Thread.currentThread().getName());
                    count++;
                    locker.notifyAll();
                }
            }
        },"B");

        t1.start();
        t2.start();
        t3.start();

    }
}
